# coding=utf-8
from __future__ import unicode_literals

from sqlalchemy import Column
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.dialects.mysql import (VARCHAR, TINYINT, INTEGER, DATE, FLOAT, DOUBLE)

from config import logging, Config


Base = declarative_base()       # 创建对象的基类


class ThemeFlowModel(Base):
    # 概念、板块资金流表
    __tablename__ = 't_theme_fund_flow'                                 # 表名

    id = Column(INTEGER, primary_key=True)                              # 主键
    fund_type = Column(TINYINT, nullable=False)                         # 类型，1表示概念，2表示行业
    date = Column(DATE, nullable=False)                                 # 时间
    name = Column(VARCHAR(20), nullable=False)                          # 概念名称
    index = Column(FLOAT(unsigned=True), nullable=False)                # 行业指数
    rose_ratio = Column(FLOAT, nullable=False)                          # 涨跌幅
    fund_amount_out = Column(FLOAT(unsigned=True), nullable=False)      # 流入资金
    fund_amount_in = Column(FLOAT(unsigned=True), nullable=False)       # 流出资金
    fund_real_in = Column(FLOAT, nullable=False)                        # 净额
    company_num = Column(INTEGER(unsigned=True), nullable=False)        # 公司家数
    leader = Column(VARCHAR(20), nullable=False)                        # 领涨股票名
    leader_rose_ratio = Column(FLOAT, nullable=False)                   # 领涨股涨幅
    leader_price = Column(FLOAT(unsigned=True), nullable=False)         # 领涨股当前价


class StockFlowModel(Base):
    # 个股资金流
    __tablename__ = 't_stock_fund_flow'                                 # 表名

    id = Column(INTEGER, primary_key=True)                              # 主键
    date = Column(DATE, nullable=False)                                 # 时间
    symbol = Column(VARCHAR(10), nullable=False)                        # 股票代码
    name = Column(VARCHAR(20), nullable=False)                          # 股票名
    price = Column(FLOAT(unsigned=True), nullable=False)                # 当前价
    rose_ratio = Column(FLOAT, nullable=False)                          # 涨跌幅
    hand_ratio = Column(FLOAT(unsigned=True), nullable=False)           # 换手率
    fund_amount_out = Column(DOUBLE(unsigned=True), nullable=False)     # 流入资金
    fund_amount_in = Column(DOUBLE(unsigned=True), nullable=False)      # 流出资金
    fund_real_in = Column(DOUBLE, nullable=False)                       # 净额
    trade_amount = Column(DOUBLE(unsigned=True), nullable=False)        # 成交额
    big_trade_in = Column(DOUBLE(unsigned=True), nullable=False)        # 大单流入


def init_table():
    # 初始化表
    from sqlalchemy import create_engine
    engine = create_engine(Config.MYSQL_URL, echo=True)
    Base.metadata.drop_all(engine)       # 删除表
    Base.metadata.create_all(engine)     # 新建表
    logging.info('初始化表成功')


if __name__ == '__main__':
    init_table()
